import requests
import re
import json

def get_one_page(url):
    headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}
    reponse = requests.get(url, headers=headers)
    if reponse.status_code == 200:
        return reponse.text
    return None

def parse_one_page(html):
    pattern = re.compile('<a.*?href="(.*?)".*?</a>', re.S)
    items = re.findall(pattern, html)
    for item in items:

        yield {
            'url': item,
        }

def write_in_file(content):
    with open('lxy.json', 'a', encoding='utf8') as f:
        f.write(json.dumps(content, ensure_ascii=False)+',\n')
        f.close()

def main():
    url = 'http://lxy.njust.edu.cn/'
    html = get_one_page(url)
    items = parse_one_page(html)
    for item  in items:
        print(item)
        write_in_file(item)

if __name__ == '__main__':
    main()
